package top.hepingan.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hepingan.java.common.Result;
import top.hepingan.java.entity.Bottle;
import top.hepingan.java.entity.Muser;
import top.hepingan.java.entity.RedPacket;
import top.hepingan.java.entity.Res.bottleres;
import top.hepingan.java.mapper.IRedPacketService;
import top.hepingan.java.mapper.MuserMapper;
import top.hepingan.java.service.Impl.service.IBottleService;
import top.hepingan.java.utils.UserUtils;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/bottle")
@Slf4j
@RequiredArgsConstructor
public class bottleController {
    private final IBottleService bottleService;
    /**
     * 优质漂流瓶
     */
    @GetMapping("/quality")
    public Result quality() {
        List<Bottle> list = bottleService.list(new LambdaQueryWrapper<Bottle>().eq(Bottle::getStatus, 2));
        return Result.ok().setData(list);
    }
    /**
     * 投递漂流瓶
     */
    @PostMapping("/throw")
    public Result throwBottle(@RequestBody Bottle bottle) {
       Long userId =  UserUtils.getCurrentLoginId();
       bottle.setUserId(userId);
        boolean success = bottleService.throwBottle(bottle);
        if (success) {
            return Result.ok("投递成功");
        }
        return Result.error("投递失败");
    }
    @Autowired
    private MuserMapper muserMapper;
    @Autowired
    private IRedPacketService redPacketService;
    /**
     * 捞取漂流瓶
     */
    @GetMapping("/pick")
    public Result pickBottle() {
        // 动态概率：70%捞瓶，30%捞红包
        double random = Math.random();
        Long userId = UserUtils.getCurrentLoginId();
        if (random < 0.7) {
            // 捞瓶
            Bottle bottle = bottleService.pickBottle();
            if(bottle == null)
            {
                return Result.error("当前没有漂流瓶可捞取");
            }
            Muser user = muserMapper.selectById(bottle.getUserId());
            if(user == null)
            {
                return Result.error("用户不存在");
            }
            bottleres bottleRes = new bottleres();
            BeanUtils.copyProperties(bottle, bottleRes);
            bottleRes.setUser(user);
            if (bottle != null) {
                return Result.ok("捞取成功").setData( bottleRes);
            }
            return Result.error("当前没有漂流瓶可捞取");
        } else {
            // 捞红包
            //获取所有可以领取的红包
            redPacketService.getRedPacket(null, userId);
            LambdaQueryWrapper<RedPacket> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RedPacket::getStatus, 0);

            List<RedPacket> redPackets = redPacketService.getBaseMapper().selectList(
                    queryWrapper
            );
            if (redPackets == null || redPackets.isEmpty()) {
                return Result.error("当前没有可领取的红包");
            }
          // 3. 随机选择一个
            int randomIndex = new Random().nextInt(redPackets.size());
            RedPacket randomRedPacket = redPackets.get(randomIndex);
            if(randomRedPacket == null)
            {
                return Result.error("当前没有可领取的红包");
            }
            return redPacketService.getRedPacket(randomRedPacket, userId);

        }

    }
    /**
     * 漂流瓶点赞
     */
    @GetMapping("/like")
    public Result likeBottle(Long bottleId) {
        Long userId = UserUtils.getCurrentLoginId();
        boolean success = bottleService.likeBottle(bottleId);
        if (success) {
            return Result.ok("点赞成功");
        }
        return Result.error("点赞失败");
    }
}
